/**
 * 表格的数据部分
 */
import {deepCopyObj, deepCopyObjArray} from '../../assets/js/help.js'
export default {
  props: {
    isResetTable: [Boolean, String], //是否需要重置表格（即重新绑定固定表头插件）
    tableBoxId: {  //表格的父元素的ID
      type: String,
      default: ''
    },
    hasCheckboxCol: {
      type: [Boolean, String],
      default: true
    },
    hasOperateCol: {
      type: [Boolean, String],
      default: true
    },
    isSetTable: {
      type: [Boolean, String],
      default: true
    },
    isNeedPrint: {
      type: [Boolean,String],
      default: true
    },
    theadsInfo: Array,
    tbodysInfo: Array,
    sortColumn: String,
    orderType: String,
    fixedCols: { // 固定列的列数， 默认固定4列
      type: [Number, String],
      default: 3
    },
    isNeedTree: {
      type: [Boolean, String],
      default: false
    },
    selectedNode : {
      type: String,
      default: ''
    }
  },
  data () {
    return {
      tHeight: 400, //表格的高度，与固定表头相关，必须设置为Number格式
      isShowSetting: false, //是否显示表格设置框
      theadShowed: [], //表格的显示列，初始默认为父组件传过来的值
      theadHidden: [], // 表格的隐藏列，初始为空
      theadsArr: [],
      orderBy: this.sortColumn,
      orderTy: this.orderType == 'ASC' ? 'ascending' : (this.orderType == 'DESC' ? 'descending' : ''),
      multipleSelection: [], //表格的多选列表
      thisRouter: this.tableBoxId || this.$route.meta, // 当前的路由标签meta
      tableId: this.tableBoxId,
      singleThWidth: 0,
      emptyText: '数据加载中...',
      isReload: false,
      isShowTree: true
    }
  },
  created () {
    this.initFilterTheads();
  },
  watch: {
    'tbodysInfo': function (val, oldVal) {
      if(val.length == 0) {
        this.emptyText = '暂无数据';
      } else {
        if(oldVal.length == 0) {
          this.emptyText = '数据加载中...';
        }
      }
      if(this.isPrintNext) {
        this.toPrintTable();
        this.isPrintNext = false;
      }
    }
  },
  methods: {
    //初始过滤 默认隐藏和显示的列
    initFilterTheads () {
      let list = deepCopyObjArray(this.theadsInfo);
      let showed = [];
      let hidden = [];
      let name = this.thisRouter;
      for(let i = 0, len = list.length; i < len; i++) {
        let initHidden = list[i].initHidden;
        let _obj = deepCopyObj(list[i])
        _obj.isShow = false
        if(initHidden) {
          hidden.push(_obj);
          list[i]['checked'] = false
        } else {
          showed.push(_obj);
          list[i]['checked'] = true
        }
      }
      this.theadsArr = deepCopyObjArray(list);
      this.theadShowed = deepCopyObjArray(showed);
      this.theadHidden = deepCopyObjArray(hidden);
      if(!(localStorage.getItem(name)) && hidden.length > 0 ) {
        this.saveSetting();
      }
    }
  }
}